{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ymhGfgFSR17k"
      },
      "source": [
        "## **Applio**\n",
        "A simple, high-quality voice conversion tool focused on ease of use and performance.\n",
        "\n",
        "[Support](https://discord.gg/urxFjYmYYh) — [GitHub](https://github.com/IAHispano/Applio) — [Terms of Use](https://github.com/IAHispano/Applio/blob/main/TERMS_OF_USE.md)\n",
        "\n",
        "<br>\n",
        "\n",
        "---\n",
        "\n",
        "<br>\n",
        "\n",
        "#### **Acknowledgments**\n",
        "\n",
        "To all external collaborators for their special help in the following areas:\n",
        "* Hina (Encryption method)\n",
        "* Poopmaster (Extra section)\n",
        "* Shirou (UV installer)\n",
        "* Bruno5430 (AutoBackup code and general notebook maintenance)\n",
        "\n",
        "#### **Disclaimer**\n",
        "By using Applio, you agree to comply with ethical and legal standards, respect intellectual property and privacy rights, avoid harmful or prohibited uses, and accept full responsibility for any outcomes, while Applio disclaims liability and reserves the right to amend these terms."
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### **Install Applio**\n",
        "If the runtime restarts, re-run the installation steps."
      ],
      "metadata": {
        "id": "NXXzfHi7Db-y"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "19LNv6iYqF6_"
      },
      "outputs": [],
      "source": [
        "# @title Mount Drive\n",
        "from google.colab import drive\n",
        "from google.colab._message import MessageError\n",
        "\n",
        "try:\n",
        "  drive.mount(\"/content/drive\")\n",
        "except MessageError:\n",
        "  print(\"❌ Failed to mount drive\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "vtON700qokuQ"
      },
      "outputs": [],
      "source": [
        "# @title Setup runtime environment\n",
        "from IPython.display import clear_output\n",
        "import codecs\n",
        "\n",
        "encoded_url = \"uggcf://tvguho.pbz/VNUvfcnab/Nccyvb/\"\n",
        "decoded_url = codecs.decode(encoded_url, \"rot_13\")\n",
        "\n",
        "repo_name_encoded = \"Nccyvb\"\n",
        "repo_name = codecs.decode(repo_name_encoded, \"rot_13\")\n",
        "\n",
        "LOGS_PATH = f\"/content/{repo_name}/logs\"\n",
        "BACKUPS_PATH = f\"/content/drive/MyDrive/{repo_name}Backup\"\n",
        "\n",
        "%cd /content\n",
        "!git config --global advice.detachedHead false\n",
        "!git clone {decoded_url} --branch 3.6.0 --single-branch\n",
        "%cd {repo_name}\n",
        "clear_output()\n",
        "\n",
        "# Install older python\n",
        "!apt update -y\n",
        "!apt install -y python3.11 python3.11-distutils python3.11-dev portaudio19-dev\n",
        "!update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2\n",
        "!update-alternatives --set python3 /usr/bin/python3.11\n",
        "from sys import path\n",
        "path.append('/usr/local/lib/python3.11/dist-packages')\n",
        "\n",
        "print(\"Installing requirements...\")\n",
        "!curl -LsSf https://astral.sh/uv/install.sh | sh\n",
        "!uv pip install -q -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu128 --index-strategy unsafe-best-match\n",
        "!uv pip install -q ngrok jupyter-ui-poll\n",
        "!npm install -g -q localtunnel &> /dev/null\n",
        "\n",
        "!python core.py \"prerequisites\" --models \"True\" --pretraineds_hifigan \"True\"\n",
        "print(\"✅ Finished installing requirements!\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### **Start Applio**"
      ],
      "metadata": {
        "id": "IlM6ll0WDuOG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# @title Sync with Google Drive\n",
        "# @markdown 💾 Run this cell to automatically Save/Load models from your mounted drive\n",
        "# @title\n",
        "# @markdown This will merge and link your `ApplioBackup` folder from gdrive to this notebook\n",
        "from IPython.display import display, clear_output\n",
        "from pathlib import Path\n",
        "\n",
        "non_bak_folders = [\"mute\", \"reference\", \"zips\", \"mute_spin\", \"mute_spin-v2\"]\n",
        "non_bak_path = \"/tmp/rvc_logs\"\n",
        "\n",
        "\n",
        "def press_button(button):\n",
        "  button.disabled = True\n",
        "\n",
        "\n",
        "def get_date(path: Path):\n",
        "  from datetime import datetime\n",
        "  return datetime.fromtimestamp(int(path.stat().st_mtime))\n",
        "\n",
        "\n",
        "def get_size(path: Path):\n",
        "  !du -shx --apparent-size \"{path}\" > /tmp/size.txt\n",
        "  return open(\"/tmp/size.txt\").readlines().pop(0).split(\"\t\")[0] + \"B\"\n",
        "\n",
        "\n",
        "def sync_folders(folder: Path, backup: Path):\n",
        "  from ipywidgets import widgets\n",
        "  from jupyter_ui_poll import ui_events\n",
        "  from time import sleep\n",
        "\n",
        "  local = widgets.VBox([\n",
        "      widgets.Label(f\"Local: {LOGS_PATH.removeprefix('/content/')}/{folder.name}/\"),\n",
        "      widgets.Label(f\"Size: {get_size(folder)}\"),\n",
        "      widgets.Label(f\"Last modified: {get_date(folder)}\")\n",
        "  ])\n",
        "  remote = widgets.VBox([\n",
        "      widgets.Label(f\"Remote: {BACKUPS_PATH.removeprefix('/content/')}/{backup.name}/\"),\n",
        "      widgets.Label(f\"Size: {get_size(backup)}\"),\n",
        "      widgets.Label(f\"Last modified: {get_date(backup)}\")\n",
        "  ])\n",
        "  separator = widgets.VBox([\n",
        "      widgets.Label(\"|||\"),\n",
        "      widgets.Label(\"|||\"),\n",
        "      widgets.Label(\"|||\")\n",
        "  ])\n",
        "  radio = widgets.RadioButtons(\n",
        "      options=[\n",
        "          \"Save local model to drive\",\n",
        "          \"Keep remote model\"\n",
        "      ]\n",
        "  )\n",
        "  button = widgets.Button(\n",
        "      description=\"Sync\",\n",
        "      icon=\"upload\",\n",
        "      tooltip=\"Sync model\"\n",
        "  )\n",
        "  button.on_click(press_button)\n",
        "\n",
        "  clear_output()\n",
        "  print(f\"Your local model '{folder.name}' is in conflict with it's copy in Google Drive.\")\n",
        "  print(\"Please select which one you want to keep:\")\n",
        "  display(widgets.Box([local, separator, remote]))\n",
        "  display(radio)\n",
        "  display(button)\n",
        "\n",
        "  with ui_events() as poll:\n",
        "    while not button.disabled:\n",
        "      poll(10)\n",
        "      sleep(0.1)\n",
        "\n",
        "  match radio.value:\n",
        "    case \"Save local model to drive\":\n",
        "      !rm -r \"{backup}\"\n",
        "      !mv \"{folder}\" \"{backup}\"\n",
        "    case \"Keep remote model\":\n",
        "      !rm -r \"{folder}\"\n",
        "\n",
        "\n",
        "if Path(\"/content/drive\").is_mount():\n",
        "  !mkdir -p \"{BACKUPS_PATH}\"\n",
        "  !mkdir -p \"{non_bak_path}\"\n",
        "\n",
        "  if not Path(LOGS_PATH).is_symlink():\n",
        "    for folder in non_bak_folders:\n",
        "      folder = Path(f\"{LOGS_PATH}/{folder}\")\n",
        "      backup = Path(f\"{BACKUPS_PATH}/{folder.name}\")\n",
        "\n",
        "      !mkdir -p \"{folder}\"\n",
        "      !mv \"{folder}\" \"{non_bak_path}\" &> /dev/null\n",
        "      !rm -rf \"{folder}\"\n",
        "      folder = Path(f\"{non_bak_path}/{folder.name}\")\n",
        "      if backup.exists() and backup.resolve() != folder.resolve():\n",
        "        !rm -r \"{backup}\"\n",
        "      !ln -s \"{folder}\" \"{backup}\" &> /dev/null\n",
        "\n",
        "    for model in Path(LOGS_PATH).iterdir():\n",
        "      if model.is_dir() and not model.is_symlink():\n",
        "        backup = Path(f\"{BACKUPS_PATH}/{model.name}\")\n",
        "\n",
        "        if model.name == \".ipynb_checkpoints\":\n",
        "          continue\n",
        "\n",
        "        if backup.exists() and backup.is_dir():\n",
        "          sync_folders(model, backup)\n",
        "        else:\n",
        "          !rm \"{backup}\"\n",
        "          !mv \"{model}\" \"{backup}\"\n",
        "\n",
        "    !rm -r \"{LOGS_PATH}\"\n",
        "    !ln -s \"{BACKUPS_PATH}\" \"{LOGS_PATH}\"\n",
        "\n",
        "    clear_output()\n",
        "    print(\"✅ Models are synced!\")\n",
        "\n",
        "  else:\n",
        "    !rm \"{LOGS_PATH}\"\n",
        "    !ln -s \"{BACKUPS_PATH}\" \"{LOGS_PATH}\"\n",
        "    clear_output()\n",
        "    print(\"✅ Models already synced!\")\n",
        "\n",
        "else:\n",
        "  print(\"❌ Drive is not mounted, skipping model syncing\")\n",
        "  print(\"To sync your models, first mount your Google Drive and re-run this cell\")"
      ],
      "metadata": {
        "cellView": "form",
        "id": "2miFQtlfiWy_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "nAlXiNYnFH9F"
      },
      "outputs": [],
      "source": [
        "# @title **Start server**\n",
        "# @markdown  ### Choose a sharing method:\n",
        "from IPython.display import clear_output\n",
        "\n",
        "method = \"gradio\"  # @param [\"gradio\", \"localtunnel\", \"ngrok\"]\n",
        "ngrok_token = \"If you selected the 'ngrok' method, obtain your auth token here: https://dashboard.ngrok.com/get-started/your-authtoken\" # @param {type:\"string\"}\n",
        "tensorboard = True #@param {type: \"boolean\"}\n",
        "\n",
        "%cd /content/{repo_name}\n",
        "clear_output()\n",
        "\n",
        "if tensorboard:\n",
        "  %load_ext tensorboard\n",
        "  %tensorboard --logdir logs --bind_all\n",
        "\n",
        "match method:\n",
        "  case 'gradio':\n",
        "    !python app.py --listen --share --client\n",
        "  case 'localtunnel':\n",
        "    !echo Password IP: $(curl --silent https://ipv4.icanhazip.com)\n",
        "    !echo\n",
        "    !lt --port 6969 & python app.py --listen --client\n",
        "  case 'ngrok':\n",
        "    import ngrok\n",
        "    ngrok.kill()\n",
        "    listener = await ngrok.forward(6969, authtoken=ngrok_token)\n",
        "    print(f\"Ngrok URL: {listener.url()}\")\n",
        "    !python app.py --listen --client"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [
        "NXXzfHi7Db-y"
      ],
      "provenance": [],
      "private_outputs": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
